package com.tcm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcm.config.AppProperties;
import com.tcm.entity.Comment;
import com.tcm.entity.CommentReport;
import com.tcm.entity.vo.CommentReportDto;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.exception.EntityInvalidException;
import com.tcm.mapper.CommentReportMapper;
import com.tcm.service.CommentReportService;
import com.tcm.service.CommentService;
import com.tcm.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Guqier
 * @description 针对表【ts_comment_report(评论举报表)】的数据库操作Service实现
 * @createDate 2023-10-12 16:40:42
 */
@Service
@RequiredArgsConstructor
public class CommentReportServiceImpl extends ServiceImpl<CommentReportMapper, CommentReport>
        implements CommentReportService {

    private final CommentService commentService;

    private final NotificationService notificationService;

    @Override
    @Transactional
    public boolean reportComment(CommentReportDto dto, Long currUserId, String currAccount) {
        CommentReport commentReport = new CommentReport();
        BeanUtils.copyProperties(dto, commentReport);
        commentReport.setAccount(currAccount);
        commentReport.setCreateUser(currUserId);
        commentReport.setUpdateUser(currUserId);
        commentReport.setIsHandle(0);

        return this.save(commentReport);
    }

    @Override
    public List<CommentReportDto> getCommentReportList() {
        return this.baseMapper.selectReportList();
    }

    @Override
    @Transactional
    public boolean handleCommentReport(Long reportId, String reason, Long currUserId, String currAccount, Integer type) {
        CommentReport commentReport = this.getById(reportId);
        if (Objects.isNull(commentReport)) {
            throw new EntityInvalidException("举报信息不存在");
        }

        if (commentReport.getIsHandle().equals(1) || commentReport.getIsHandle().equals(2)) {
            throw new EntityInvalidException("举报信息已处理");
        }
        if (type.equals(1)) {
            // 处理评论
            Comment comment = commentService.getById(commentReport.getCommentId());
            if (Objects.nonNull(comment)) {
                if (!commentService.removeComment(comment.getId())) {
                    throw new DatabaseOperationException("删除评论失败");
                }
            }

            commentReport.setIsHandle(1);
            commentReport.setHandleAccount(currAccount);
            commentReport.setHandleReason(reason);
            commentReport.setHandleTime(new Date());
            commentReport.setUpdateUser(currUserId);
            if (!this.updateById(commentReport)) {
                throw new DatabaseOperationException("更新举报信息失败");
            }

            // 添加通知
            if (!notificationService.addNotificationForStudent("评论删除通知",
                    String.format("您的评论【%s】被管理员删除，原因：%s",
                            comment.getContent(),
                            reason),
                    AppProperties.NotificationType.IMPORTANT, 0, comment.getAccount(), currUserId)
                    || !notificationService.addNotificationForStudent("举报成功",
                    String.format("您对评论【%s】的举报【%s】已被处理，原因：%s",
                            comment.getContent(),
                            commentReport.getReportContent(),
                            reason),
                    AppProperties.NotificationType.IMPORTANT, 0, commentReport.getAccount(), currUserId)) {
                throw new DatabaseOperationException("添加通知失败");
            }
            return true;
        } else {
            Comment comment = commentService.getById(commentReport.getCommentId());
            if (Objects.isNull(comment)) {
                throw new EntityInvalidException("评论不存在");
            }

            commentReport.setIsHandle(2);
            commentReport.setHandleAccount(currAccount);
            commentReport.setHandleReason(reason);
            commentReport.setHandleTime(new Date());
            commentReport.setUpdateUser(currUserId);
            if (!this.updateById(commentReport)) {
                throw new DatabaseOperationException("更新举报信息失败");
            }

            // 添加通知
            if (!notificationService.addNotificationForStudent("举报被驳回",
                    String.format("您的对评论【%s】的举报【%s】已被驳回，原因：%s",
                            comment.getContent(),
                            commentReport.getReportContent(),
                            reason),
                    AppProperties.NotificationType.IMPORTANT, 0, commentReport.getAccount(), currUserId)) {
                throw new DatabaseOperationException("添加通知失败");
            }
            return true;
        }

    }

}




